package com.wosiliujing.learning.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wosiliujing.learning.admin.po.SysOauthClientDetails;
import com.wosiliujing.learning.annotation.SysLog;
import com.wosiliujing.learning.constant.CommonConstants;
import com.wosiliujing.learning.service.SysOauthClientDetailService;
import com.wosiliujing.learning.util.R;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * @author: liujing
 * @date: 2019/5/19 18:22
 * @description:
 */
@RestController
@AllArgsConstructor
@RequestMapping("/client")
public class SysOauthClientDetailController {

    private final SysOauthClientDetailService sysOauthClientDetailService;

    /**
     * 分页查询数据
     * @param page 分页参数
     * @param sysOauthClientDetails 条件
     * @return 分页数据
     */
    @GetMapping("/page")
    public R findByPage(Page page, SysOauthClientDetails sysOauthClientDetails){
        return R.data(sysOauthClientDetailService.page(page,Wrappers.query(sysOauthClientDetails)));
    }

    /**
     * 保存客户端信息
     * @param sysOauthClientDetails 客户端信息
     * @return 保存结果
     */
    @SysLog("保存客户端信息")
    @PreAuthorize("@pms.hasPermisssion('sys_client_add')")
    @PostMapping("/save")
    public R save(@RequestBody SysOauthClientDetails sysOauthClientDetails){
        return sysOauthClientDetailService.save(sysOauthClientDetails)?R.ok():R.error(CommonConstants.SAVE_FAIL);
    }

    /**
     * 更新客户端信息
     * @param sysOauthClientDetails 客户端信息
     * @return 更新结果
     */
    @SysLog("更新客户端信息")
    @PreAuthorize("@pms.hasPermisssion('sys_client_edit')")
    @PutMapping("/update")
    public R update(@RequestBody SysOauthClientDetails sysOauthClientDetails){
        return sysOauthClientDetailService.updateById(sysOauthClientDetails)?R.ok():R.error(CommonConstants.UPDATE_FAIL);
    }

    /**
     * 删除
     * @param id id
     * @return 删除结果
     */
    @SysLog("删除客户端")
    @PreAuthorize("@pms.hasPermisssion('sys_client_del')")
    @DeleteMapping("/delete/{id}")
    public R delete(@PathVariable String id){
        return  sysOauthClientDetailService.removeById(id)?R.ok():R.error(CommonConstants.DELETE_FAIL);
    }



}
